package com.ptu.spzx.config;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ptu.spzx.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;


@Component
public class MyMetaObjectHander implements MetaObjectHandler {
    //插入时的填充策略
    @Override
    public void insertFill(MetaObject metaObject) {
        Long userId=null;
        try{
             userId= SecurityUtils.getUserId();
        }catch (Exception e){
            e.printStackTrace();
            userId=-1L;
        }
        this.setFieldValByName("createTime",LocalDateTime.now(),metaObject);
        this.setFieldValByName("creator",userId.toString(),metaObject);
        this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
        this.setFieldValByName("updater",userId.toString(),metaObject);
    }

    //更新时的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        Long userId=null;
        try{
            userId= SecurityUtils.getUserId();
        }catch (Exception e){
            e.printStackTrace();
            userId=-1L;
        }
        this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
        this.setFieldValByName("updater",userId.toString(),metaObject);
    }
}
